package com.ys.nms.module.business.controller.admin.sensors;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;

import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;

import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.sensors.vo.*;
import com.ys.nms.module.business.dal.dataobject.sensors.SensorsDO;
import com.ys.nms.module.business.service.sensors.SensorsService;

@Tag(name = "管理后台 - 传感器")
@RestController
@RequestMapping("/business/sensors")
@Validated
public class SensorsController {

    @Resource
    private SensorsService sensorsService;

    @PostMapping("/create")
    @Operation(summary = "创建传感器")
    public CommonResult<Integer> createSensors(@Valid @RequestBody SensorsSaveReqVO createReqVO) {
        return success(sensorsService.createSensors(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新传感器")
    public CommonResult<Boolean> updateSensors(@Valid @RequestBody SensorsSaveReqVO updateReqVO) {
        sensorsService.updateSensors(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除传感器")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSensors(@RequestParam("id") Integer id) {
        sensorsService.deleteSensors(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除传感器")
    public CommonResult<Boolean> deleteSensorsList(@RequestParam("ids") List<Integer> ids) {
        sensorsService.deleteSensorsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得传感器")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SensorsRespVO> getSensors(@RequestParam("id") Integer id) {
        SensorsDO sensors = sensorsService.getSensors(id);
        return success(BeanUtils.toBean(sensors, SensorsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得传感器分页")
    public CommonResult<PageResult<SensorsRespVO>> getSensorsPage(@Valid SensorsPageReqVO pageReqVO) {
        PageResult<SensorsDO> pageResult = sensorsService.getSensorsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SensorsRespVO.class));
    }

    @Slave
    @TransMethodResult
    @GetMapping("/getCurrent")
    @Operation(summary = "获得传感器分页")
    public CommonResult<SensorsRespVO> getCurrent(@Valid SensorsPageReqVO pageReqVO) {
        SensorsDO current = sensorsService.getCurrent(pageReqVO);
        return success(BeanUtils.toBean(current, SensorsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出传感器 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSensorsExcel(@Valid SensorsPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SensorsDO> list = sensorsService.getSensorsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "传感器.xls", "数据", SensorsRespVO.class,
                BeanUtils.toBean(list, SensorsRespVO.class));
    }

}